/*
 * ListAccelerator.java
 *
 * Created on September 22, 2007, 10:43 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package Raytracing.Accelerators;
import Raytracing.Intersectable;
import Raytracing.Ray;
import processing.core.*;
import java.util.*;


/**
 *
 * @author Matt
 */
public class ListAccelerator extends Accelerator{
    
    
    /** Creates a new instance of ListAccelerator */
    public ListAccelerator() {
        objects = new ArrayList();
    }
    
    public void validate(){
        
    }
 
    public void add(Intersectable newObject){
        objects.add(newObject);
    }
    
    public void draw(PApplet g){
    
        Intersectable temp;
        for (int i = 0; i < objects.size(); i++ ){
            temp = (Intersectable)objects.get(i);
            temp.draw(g);

        }
    
    }
    public void draw(PApplet g, PVector camDirection) {
        

        Intersectable temp;
        for (int i = 0; i < objects.size(); i++ ){
            temp = (Intersectable)objects.get(i);
            temp.draw(g,camDirection);
        }
        
        

    }     
    
    public boolean findNearest(Ray state, int ID){
    
        float min = 1e6f;
        
        Intersectable obj, nearest = null;
        
        for (int i =0; i < objects.size(); i++){
            obj = (Intersectable)objects.get(i);
            if (obj.intersect(state)){
                
                if (state.distance < min){
                    min = state.distance;
                    nearest = obj;
                    
                }
            }
        }        
        if (nearest == null)return false;
        else{
            state.distance = min;
            state.occluder = nearest;
            return true;
        }
    }
    
}
